import pika

def send_rabbitmq_message():
    # 配置 RabbitMQ 连接
    connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
    channel = connection.channel()

    # 声明交换机（确保存在）
    channel.exchange_declare(
        exchange='balance_submit',
        exchange_type='direct',
        durable=True
    )

    # 声明队列并绑定到交换机
    channel.queue_declare(queue='exchange.dcp.balance.submit.balance_submit', durable=True)
    channel.queue_bind(
        queue='exchange.dcp.balance.submit.balance_submit',
        exchange='balance_submit',
        routing_key='route.dcp.balance.submit'
    )

    # 启用发布确认
    channel.confirm_delivery()

    # 发送消息
    # message = '你的消息内容'
    # try:
    #     channel.basic_publish(
    #         exchange='exchange.ubs.elect.receipt',
    #         routing_key='route.ubs.elect.receipt',
    #         body=message,
    #         properties=pika.BasicProperties(
    #             delivery_mode=2,  # 持久化消息
    #         ),
    #         mandatory=True  # 确保消息被路由
    #     )
    #     print("消息发送成功")
    # except pika.exceptions.UnroutableError:
    #     print("消息未被路由到任何队列")
    # except Exception as e:
    #     print(f"发送消息时出错: {e}")
    # finally:
    #     connection.close()

if __name__ == "__main__":
    send_rabbitmq_message()
